package com.eduction.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eduction.bean.SessionUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eduction.bean.ExpertAndIdentity;
import com.eduction.common.Hash;
import com.eduction.common.ReturnCode;
import com.eduction.common.dbutil.page.Page;
import com.eduction.modules.system.entity.Expert;
import com.eduction.modules.system.entity.ExpertGroupMap;
import com.eduction.modules.system.entity.Group;
import com.eduction.modules.system.entity.School;
import com.eduction.modules.system.entity.SchoolGroupMap;
import com.eduction.resource.BaseController;
import com.eduction.service.SchoolService;
import com.eduction.service.SiteInspectionExpertService;

@Controller
@RequestMapping("/siteInspection")
public class SiteInspectionExpertController extends BaseController {
    private static final Log logger = LogFactory.getLog(SiteInspectionExpertController.class);
    @Autowired
    private SiteInspectionExpertService siteInspectionExpertService;

    @Autowired
    private SchoolService schoolservice;

    /**
     * 添加专家
     */
    @RequestMapping(value = "/addExpert", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addExpert(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String name = map.get("name");
        String code = map.get("code");
        String title = map.get("title");
        String post = map.get("post");
        String field = map.get("field");
        String officephone = map.get("officephone");
        String phone = map.get("phone");
        String email = map.get("email");
        String workunit = map.get("workunit");
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if (name == null || StringUtils.isBlank(name)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "姓名为空");
        }
        if (code == null || StringUtils.isBlank(code)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "编号为空");
        }
        Expert expert = new Expert();
        expert.setS_name(StringUtils.trim(name));
        expert.setS_expert_code(StringUtils.trim(code));
        expert.setS_title(StringUtils.trim(title));
        expert.setS_post(StringUtils.trim(post));
        expert.setS_field(StringUtils.trim(field));
        expert.setS_officephone(StringUtils.trim(officephone));
        expert.setS_phone(StringUtils.trim(phone));
        expert.setS_email(StringUtils.trim(email));
        expert.setS_workunit(workunit);
        expert.setS_struts("0");
        expert.setS_strutss("0");
        expert.setS_password(Hash.sha2InHex("111111"));
        expert.setS_create_time(createTime);
        try {
            this.siteInspectionExpertService.saveExpert(expert);
            return toResponse(null, ReturnCode.SUCCESS, "添加专家成功");
        } catch (RuntimeException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "添加专家失败");
        }
    }

    /**
     * 删除专家
     */
    @RequestMapping(value = "/removeExpert", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> removeExpert(HttpServletRequest request, @RequestParam String pid) {

        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        if (pid == null || StringUtils.isBlank(pid)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家编号为空");
        }

        this.siteInspectionExpertService.removeExpert(pid);

        return toResponse(null, ReturnCode.SUCCESS, "删除专家成功");
    }

    /**
     * 修改专家信息
     */
    @RequestMapping(value = "/modifyExpert", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> modifyExpert(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        String name = map.get("name");
        String code = map.get("code");
        String title = map.get("title");
        String post = map.get("post");
        String field = map.get("field");
        String officephone = map.get("officephone");
        String phone = map.get("phone");
        String email = map.get("email");
        String workunit = map.get("workunit");
        if (pid == null || StringUtils.isBlank(pid)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "ID为空");
        }
        if (name == null || StringUtils.isBlank(name)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "姓名为空");
        }
        if (code == null || StringUtils.isBlank(code)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "编号为空");
        }
        try {
            Expert expert = this.siteInspectionExpertService.queryExpertById(pid);
            expert.setS_name(name);
            expert.setS_expert_code(code);
            expert.setS_title(title);
            expert.setS_post(post);
            expert.setS_field(field);
            expert.setS_officephone(officephone);
            expert.setS_phone(phone);
            expert.setS_email(email);
            expert.setS_workunit(workunit);

            this.siteInspectionExpertService.saveExpert(expert);
            return toResponse(null, ReturnCode.SUCCESS, "修改专家信息成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改专家信息失败");
        }
    }

    /**
     * 查询单个专家信息
     */
    @RequestMapping(value = "/queryExpert", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<String> queryExpertById(HttpServletRequest request, @RequestParam String pid) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }


        if (pid == null || StringUtils.isBlank(pid)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "Id为空");
        }
        try {
            Expert expert = this.siteInspectionExpertService.queryExpertById(pid);
            return toResponse(expert, ReturnCode.SUCCESS, "查询单个专息成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询单个专家失败");
        }
    }

    /**
     * 分页查询专家
     */
    @RequestMapping(value = "/queryExperts", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryExpertByPage(HttpServletRequest request, @RequestBody Map<String, String> map) {

        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        if (map.get("pageNumber") == null || map.get("pageSize") == null || StringUtils.isBlank(map.get("pageNumber")) || StringUtils.isBlank(map.get("pageSize"))) {
            return toResponse(null, ReturnCode.FAIL, "参数不能为空!");
        }
        try {
            Page<Expert> page = this.siteInspectionExpertService.queryExpertByPage(map);
            return toResponse(page, ReturnCode.SUCCESS, "分页查询成功!");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "分页查询失败!");
        }
    }

    /**
     * 分页查询专家组
     */
    @RequestMapping(value = "/queryGroups", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryGroupByPage(HttpServletRequest request, @RequestBody Map<String, String> map) {

        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        if (map.get("pageNumber") == null || map.get("pageSize") == null || StringUtils.isBlank(map.get("pageNumber")) || StringUtils.isBlank(map.get("pageSize"))) {
            return toResponse(null, ReturnCode.FAIL, "参数不能为空!");
        }
        String groupType = map.get("group_type");
        try {
            Page<Group> page = this.siteInspectionExpertService.selectGroupByPage(map, Integer.valueOf(groupType));
            return toResponse(page, ReturnCode.SUCCESS, "分页查询成功!");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "分页查询失败!");
        }
    }
	
	/*private String pid;//
	private String s_name;//组名
	private String s_state;//状态 0-未开始,1-开始,3-结束
	private String s_type;//类型 1-材料 2-现场
	private String s_create_time;//*/

    /**
     * 添加专家组
     */
    @RequestMapping(value = "/addGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {

        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }
        String name = map.get("name");
        String type = map.get("type");
        if (name == null || StringUtils.isBlank(name)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组名不能为空");
        }
        if (type == null || StringUtils.isBlank(type)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "类型不能为空");
        }
        Group group = new Group();
        group.setS_name(name);
        group.setS_type(type);
        group.setS_state("0");
        group.setS_create_time(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        this.siteInspectionExpertService.saveGroup(group);
        return toResponse(null, ReturnCode.SUCCESS, "添加专家组成功");
    }

    /**
     * 修改专家组名称
     */
    @RequestMapping(value = "/updateGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updateGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        String name = map.get("name");
        if (pid == null || StringUtils.isBlank(pid)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (name == null || StringUtils.isBlank(name)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组名不能为空");
        }
        try {
            Group group = this.siteInspectionExpertService.selectGroupById(pid);
            group.setS_name(name);
            this.siteInspectionExpertService.saveGroup(group);
            return toResponse(null, ReturnCode.SUCCESS, "添加专家组成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改专家组信息失败");
        }
    }


    /**
     * 修改专家组状态
     */
    @RequestMapping(value = "/updateGroupState", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> updateGroupState(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String pid = map.get("pid");
        String state = map.get("state");
        if (pid == null || StringUtils.isBlank(pid)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (state == null || StringUtils.isBlank(state)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "状态不能为空");
        }
        try {
            Group group = this.siteInspectionExpertService.selectGroupById(pid);
            group.setS_state(state);
            this.siteInspectionExpertService.saveGroup(group);
            return toResponse(null, ReturnCode.SUCCESS, "修改状态成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "修改状态失败");
        }
    }

    /**
     * 查询未绑定专家组的学校
     */
    @RequestMapping(value = "/querySchoolNotBand", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> querySchoolNotBand(HttpServletRequest request, @RequestBody Map<String, String> map) {
        try {
            String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
            if(StringUtils.isEmpty(user_type)){
                return toResponse(null,ReturnCode.FAIL,"暂无权限！");
            }
            String groupType = map.get("group_type");
            List<School> schools = null;
            if ("1".equals(groupType)) {
                schools = this.schoolservice.querySchoolNotBand2();
            }
            if ("2".equals(groupType)) {
                schools = this.schoolservice.querySchoolNotBand();
            }
            return toResponse(schools, ReturnCode.SUCCESS, "查询成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败");
        }
    }

    /**
     * 为专家组分配学校
     */
    @RequestMapping(value = "/setGroupToSchool", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> setGroupToSchool(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }
        String groupId = map.get("groupId");
        String schoolList = map.get("schoolList");
        String groupType = map.get("group_type");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (schoolList == null || StringUtils.isBlank(schoolList)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校不能为空");
        }
        try {
            String[] schools = schoolList.split(":");
            SchoolGroupMap schoolGroupMap = null;
            if ("1".equals(groupType)) {
                for (String s : schools) {
                    schoolGroupMap = new SchoolGroupMap();
                    schoolGroupMap.setS_groupId(groupId);
                    schoolGroupMap.setS_schoolId(s);
                    schoolGroupMap.setS_state("1");
                    this.siteInspectionExpertService.setGroupToSchool2(schoolGroupMap);
                }
            }
            if ("2".equals(groupType)) {
                for (String s : schools) {
                    schoolGroupMap = new SchoolGroupMap();
                    schoolGroupMap.setS_groupId(groupId);
                    schoolGroupMap.setS_schoolId(s);
                    schoolGroupMap.setS_state("1");
                    this.siteInspectionExpertService.setGroupToSchool(schoolGroupMap);
                }
            }
            return toResponse(null, ReturnCode.SUCCESS, "分配学校成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "分配学校失败");
        }
    }

    /**
     * 删除专家组与学校的关系
     */
    @RequestMapping(value = "/deleteSchoolInGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteSchoolInGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        String schoolList = map.get("schoolList");
        String groupType = map.get("group_type");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (schoolList == null || StringUtils.isBlank(schoolList)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "学校不能为空");
        }
        try {
            if ("2".equals(groupType)) {
                String[] list = schoolList.split(":");
                for (String s : list) {
                    this.siteInspectionExpertService.deleteSchoolInGroup(s, groupId);
                }
            }
            if ("1".equals(groupType)) {
                String[] list = schoolList.split(":");
                for (String s : list) {
                    this.siteInspectionExpertService.deleteSchoolInGroup2(s, groupId);
                }
            }
            return toResponse(null, ReturnCode.SUCCESS, "删除组与学校关系成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除组与学校关系失败");
        }
    }

    /**
     * 删除专家组
     *
     * @param request
     * @param map
     * @return
     */
    @Transactional
    @RequestMapping(value = "/deleteGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("group_id");
        String groupType = map.get("group_type");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        try {

            this.siteInspectionExpertService.deleteGroup(groupId,groupType);

            return toResponse(null, ReturnCode.SUCCESS, "删除专家组成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除专家组失败");
        }
    }


    /**
     * 查询某个专家组下的专家
     */
    @RequestMapping(value = "/queryExpertInGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryExpertInGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家组ID不能为空");
        }
        try {
            List<ExpertAndIdentity> experts = this.siteInspectionExpertService.selectExpertByGroup(groupId);
            return toResponse(experts, ReturnCode.SUCCESS, "查询某个专家组成员成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询某个专家组成员失败");
        }
    }

    /**
     * 查询某个专家组下的学校
     */
    @RequestMapping(value = "/querySchoolByGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> querySchoolByGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家组ID不能为空");
        }
        try {
            List<School> schools = this.siteInspectionExpertService.querySchoolByGroup(groupId);
            return toResponse(schools, ReturnCode.SUCCESS, "查询专家组下学校成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询专家组下学校失败");
        }
    }

    /**
     * 查询不在专家组和已经结束的专家
     */
    @RequestMapping(value = "/queryExpertNotInGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> queryExpertNotInGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupType = map.get("group_type");
        String param = null;
        if ("1".equals(groupType)) {
            param = "s_struts";
        } else {
            param = "s_strutss";
        }
        try {
            List<Expert> experts = this.siteInspectionExpertService.selectExpertIsNotGroup(param);
            return toResponse(experts, ReturnCode.SUCCESS, "查询成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "查询失败");
        }
    }

    /**
     * 分配专家组成员权限
     */
    @RequestMapping(value = "/setPowerOfExpert", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> setPowerOfExpert(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        String expertId = map.get("expertId");
        String identity = map.get("identity");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (expertId == null || StringUtils.isBlank(expertId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家ID不能为空");
        }
        if (identity == null || StringUtils.isBlank(identity)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "权限不能为空");
        }
        try {
            this.siteInspectionExpertService.updateIdentityInGroup(groupId, expertId, identity);
            return toResponse(null, ReturnCode.SUCCESS, "分配权限成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "分配权限失败");
        }
    }

    /**
     * 为专家组添加成员
     */
    @RequestMapping(value = "/addExpertToGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> addExpertToGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        String expertlList = map.get("expertlList");
        String groupType = map.get("group_type");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (expertlList == null || StringUtils.isBlank(expertlList)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家不能为空");
        }
        String[] es = expertlList.split(":");
        try {
            //ExpertGroupMap expertGroupMap = null;
            if ("1".equals(groupType)) {
                for (String e : es) {
                    ExpertGroupMap expertGroupMap = new ExpertGroupMap();
                    expertGroupMap.setS_group_id(groupId);
                    expertGroupMap.setS_expert_id(e);
                    expertGroupMap.setS_identity("1");
                    this.siteInspectionExpertService.saveExpertGroupMap2(expertGroupMap);
                }
            } else {
                for (String e : es) {
                    ExpertGroupMap expertGroupMap = new ExpertGroupMap();
                    expertGroupMap.setS_group_id(groupId);
                    expertGroupMap.setS_expert_id(e);
                    expertGroupMap.setS_identity("1");
                    this.siteInspectionExpertService.saveExpertGroupMap(expertGroupMap);
                }
            }
            return toResponse(null, ReturnCode.SUCCESS, "分配学校成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "分配学校失败");
        }
    }


    /**
     * 删除专家组成员
     */
    @RequestMapping(value = "/deleteExpertInGroup", produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<String> deleteExpertInGroup(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String user_type = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_TYPE);
        if(StringUtils.isEmpty(user_type)){
            return toResponse(null,ReturnCode.FAIL,"暂无权限！");
        }

        String groupId = map.get("groupId");
        String expertlList = map.get("expertlList");
        String groupType = map.get("group_type");
        if (groupId == null || StringUtils.isBlank(groupId)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "组ID不能为空");
        }
        if (expertlList == null || StringUtils.isBlank(expertlList)) {
            return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "专家不能为空");
        }
        try {

            String[] es = expertlList.split(":");
            if ("1".equals(groupType)) {
                for (String e : es) {
                    this.siteInspectionExpertService.deleteExpertInGroup2(e, groupId);
                }
            } else {
                for (String e : es) {
                    this.siteInspectionExpertService.deleteExpertInGroup(e, groupId);
                }
            }
            return toResponse(null, ReturnCode.SUCCESS, "删除成功");
        } catch (SQLException e) {
            logger.error(e);
            return toResponse(null, ReturnCode.FAIL, "删除失败");
        }

    }
}
